﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using ColumnStyle = Infragistics.Win.UltraWinGrid.ColumnStyle;

namespace Dashboard.ProductBonus
{
    public partial class FormShiftSchedule : Form
    {

        private ValueList _shiftValues = new ValueList();
        public FormShiftSchedule()
        {
            InitializeComponent();
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "Team";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["Team"].CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
            //e.Layout.Bands[0].Columns["Shift"].CellClickAction = CellClickAction.EditAndSelectText;


            _shiftValues.ValueListItems.Clear();
            _shiftValues.ValueListItems.Add("08:00-16:30", "A");
            _shiftValues.ValueListItems.Add("18:00-23:30", "B");
            _shiftValues.ValueListItems.Add("00:30-08:00", "C");

            e.Layout.Bands[0].Columns["Shift"].ValueList = _shiftValues;
            e.Layout.Bands[0].Columns["Shift"].Style = ColumnStyle.DropDown;
            e.Layout.Bands[0].Columns["FromTime"].CellActivation = Activation.NoEdit;
            e.Layout.Bands[0].Columns["ToTime"].CellActivation = Activation.NoEdit;
            e.Layout.Bands[0].Columns["Shift"].Header.Appearance.ForeColor = Color.Red;
        }

        private void FormShiftSchedule_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            var table = GenerateTableSchema();
            table.Rows.Add(new object[] { "Team1", "A", "08:00", "16:30" });
            table.Rows.Add(new object[] { "Team2", "B", "18:00", "23:30" });
            table.Rows.Add(new object[] { "Team3", "C", "00:30", "08:00" });
            table.Rows.Add(new object[] { "Team4", "A", "08:00", "16:30" });
            table.Rows.Add(new object[] { "Team5", "A", "08:00", "16:30" });
            table.Rows.Add(new object[] { "Team6", "A", "08:00", "16:30" });
            table.Rows.Add(new object[] { "Team7", "", "", "" });
            table.Rows.Add(new object[] { "Team8", "", "", "" });
            table.Rows.Add(new object[] { "Team9", "", "", "" });
            this.ultraGridMain.DataSource = table;
            this.ultraGridMain.DataBind();
        }

        private void ultraGridMain_CellChange(object sender, CellEventArgs e)
        {
            this.ultraGridMain.UpdateData();
            if (e.Cell.Column.Key == "Shift")
            {
                var shift = e.Cell.Value.ToString();
                e.Cell.Row.Cells["ToTime"].Value = shift.Split('-')[0];
                e.Cell.Row.Cells["FromTime"].Value = shift.Split('-')[1];
            }
            this.ultraGridMain.UpdateData();
        }


        private void toolStripButtonReset_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            var table = GenerateTableSchema();
            foreach (var row in this.ultraGridMain.Rows)
            {
                var team = row.Cells["Team"].Value.ToString();
                var shift = row.Cells["Shift"].Text;
                var fromTime = row.Cells["FromTime"].Value.ToString();
                var toTime = row.Cells["ToTime"].Value.ToString();
                table.Rows.Add(new object[] {team, shift, fromTime, toTime});
            }
            //save to database
            MessageBox.Show("Save data successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private DataTable GenerateTableSchema()
        {
            var dataTable = new DataTable();
            dataTable.Columns.Add("Team");
            dataTable.Columns.Add("Shift");
            dataTable.Columns.Add("FromTime");
            dataTable.Columns.Add("ToTime");
            return dataTable;
        }
    }
}
